
// have to implement diagonal flips, show next moves, and make sure that only moves that are legal are allowed.
#include <iostream>

using namespace std;

class Gameboard
{
	private:
		char board[10][10];
		char tempboard[10][10];
		char piece;
		bool toggleDisplay;
		void flipUp(short row, short column);
		void flipDown(short row, short column);
		void flipRight(short row, short column);
		void flipLeft(short row, short column);

	public:
		void copy(char source[10][10], char destination[10][10]);
		void setup();
		void black();
		void white();
		void findAvailable(short row, short column);
		void showNextPos();
		bool move(short row, char column);
		void display();
		void showDisplay(bool a);
		char currentColor();
		void flip(short row,short column);
		//int findpositions(char color);

};

void Gameboard:: copy(char source[10][10], char destination[10][10])// copies the board
{
	for(short i=0; i<10; i++)
	{
		for(short j=0; j<10; j++)
		{
			destination[i][j] = source[i][j];
		}
	}
}

void Gameboard:: setup()// initializes the board
{
	black();
	
	
	//setup board to have z along the edges and _ inside
	for (int i=0; i<10; i++)
	{
		//border
		board[0][i] = 'z';
		board[i][0] = 'z';
		board[9][i] = 'z';
		board [i][9] = 'z';
		
		{
			for (int j=0; j<9; j++)
			{
				if(i>0 && j>0 && j<9 && i<9)
				{
					board[i][j] = '_';
				}
			}
		}
	}
	
	//initialize the middle 4
	board[4][4] = 'O';
	board[4][5] = '@';
	board[5][4] = '@';
	board[5][5] = 'O';
	
	
}

void Gameboard:: black()
{
	piece = '@';
}

void Gameboard:: white()
{
	piece = 'O';
}


void Gameboard:: findAvailable(short row, short column)
{
	char opp;
	short i;
	short j;
	if(piece == 'O')
		opp = '@';
	else if(piece == '@')
		opp = 'O';
		
		
	char nextcell = 'a';

	//TRAVERSE UP
	i = row-1;
	nextcell = tempboard[i][column];
	while (nextcell != 'z')
	{
		if(nextcell == opp)
		{
			nextcell = tempboard[--i][column];
			if(nextcell == '_')
				tempboard[i][column] = '*';
		}
		else
		{
			break;
		}
	}
	
	//TRAVERSE UP-RIGHT
	i = row-1;
	j = column+1;
	nextcell = tempboard[i][j];
	while (nextcell != 'z')
	{
		if(nextcell == opp)
		{
			nextcell = tempboard[--i][++j];
			if(nextcell == '_')
				tempboard[i][j] = '*';
		}
		else
		{
			break;
		}
	}
	
	//TRAVERSE RIGHT
	i = column+1;
	nextcell = tempboard[row][i];
	while (nextcell != 'z')
	{
		if(nextcell == opp)
		{
			nextcell = tempboard[row][++i];
			if(nextcell == '_')
				tempboard[row][i] = '*';
		}
		else
		{
			break;
		}
	}
	
	//TRAVERSE DOWN-RIGHT
	i = row+1;
	j = column+1;
	nextcell = tempboard[i][j];
	while (nextcell != 'z')
	{
		if(nextcell == opp)
		{
			nextcell = tempboard[++i][++j];
			if(nextcell == '_')
				tempboard[i][j] = '*';
		}
		else
		{
			break;
		}
	}
	
	//TRAVERSE DOWN
	i = row+1;
	nextcell = tempboard[i][column];
	while (nextcell != 'z')
	{
		if(nextcell == opp)
		{
			nextcell = tempboard[++i][column];
			if(nextcell == '_')
				tempboard[i][column] = '*';
		}
		else
		{
			break;
		}
	}
	
	//TRAVERSE DOWN-LEFT
	i = row+1;
	j = column-1;
	nextcell = tempboard[i][j];
	while (nextcell != 'z')
	{
		if(nextcell == opp)
		{
			nextcell = tempboard[++i][--j];
			if(nextcell == '_')
				tempboard[i][j] = '*';
		}
		else
		{
			break;
		}
	}
	
	
	//TRAVERSE LEFT
	i = column-1;
	nextcell = tempboard[row][i];
	while (nextcell != 'z')
	{
		if(nextcell == opp)
		{
			nextcell = tempboard[row][--i];
			if(nextcell == '_')
				tempboard[row][i] = '*';
		}
		else
		{
			break;
		}
	}
	
	
	
	//TRAVERSE UP-LEFT
	i = row-1;
	j = column-1;
	nextcell = tempboard[i][j];
	while (nextcell != 'z')
	{
		if(nextcell == opp)
		{
			nextcell = tempboard[--i][--j];
			if(nextcell == '_')
				tempboard[i][j] = '*';
		}
		else
		{
			break;
		}
	}
}

void Gameboard:: showNextPos()
{	
	copy(board, tempboard);
	
	//traverse board
	//when piece found, scan in every direction until z
	//looking for opposite color then empty
	//fill that empty with *
	
	//traverse board
	for(int i=0; i<10; i++)
	{
		for(int j=0; j<10; j++)
		{
			//when piece found
			if(board[i][j] == currentColor())
			{
				cout << "checking piece [" << i << "][" << j << "]" << endl;
				findAvailable(i,j);
			}
		}
	}
}
bool Gameboard:: move(short row, char column)
{
	// if (toggleDisplay== true)
	// {
		// display();
	// }
	
	int icolumn = 0;
	
	if(column == 'a')
		icolumn = 1;
	if(column == 'b')
		icolumn = 2;
	if(column == 'c')
		icolumn = 3;
	if(column == 'd')
		icolumn = 4;
	if(column == 'e')
		icolumn = 5;
	if(column == 'f')
		icolumn = 6;
	if(column == 'g')
		icolumn = 7;
	if(column == 'h')
		icolumn = 8;
	
	board[row][icolumn] = piece;
	flip(row, icolumn);
	
	if(piece == '@')
		white();
	
	else if(piece == 'O')
		black();
		
		
		
	if (toggleDisplay== true)
	{
		display();
	}
	
}

void Gameboard:: display()
{
	cout << "    a b c d e f g h" << endl;
	for(int i=0; i<10; i++)
	{
		cout << i << "|";
		for(int j=0; j<10; j++)
		{
			cout << board[i][j] << "|";
		}
		cout << endl;
	}
}

void Gameboard:: showDisplay(bool a)
{
	toggleDisplay = a;
}

char Gameboard:: currentColor()
{
	return piece;
}

// int findpositions(char color)// working on this
// {
	// for(int i=1; i<9; i++)
	// {
		// for(int j=1; j<9; j++)
		// {
			
		// }
// }

void Gameboard:: flip(short row, short column)
{

	flipUp(row,column);
	flipDown(row,column);
	flipRight(row,column);
	flipLeft(row,column);
}
	
void Gameboard:: flipUp(short row, short column)
{
	char opp;
	char nextcell = 'a';
	if(piece == 'O')
		opp = '@';
	else if(piece == '@')
		opp = 'O';
	short i = row-1;
	nextcell = board[i][column];
	while (nextcell != 'z')
	{
		if(nextcell == opp)
		{
			nextcell = board[--i][column];
			if(nextcell == piece)
			{
				for(int j=i;j<row;j++)
				{ 
				board[j][column]=piece;
				}
				break;
			}
		}
		else
		{
			break;
		}
	}
}
void Gameboard:: flipDown(short row, short column)
{
	char opp;
	char nextcell = 'a';
	if(piece == 'O')
		opp = '@';
	else if(piece == '@')
		opp = 'O';
	short i = row+1;
	nextcell = board[i][column];
	while (nextcell != 'z')
	{
		if(nextcell == opp)
		{
			nextcell = board[++i][column];
			if(nextcell == piece)
			{
				for(int j=row;j<=i;j++)
				{ 
				board[j][column]=piece;
				}
				break;
			}
		}
		else
		{
			break;
		}
	}
}
void Gameboard:: flipRight(short row, short column)
{
	char opp;
	char nextcell = ' ';
	if(piece == 'O')
		opp = '@';
	else if(piece == '@')
		opp = 'O';
	short i = column+1;
	nextcell = board[row][i];
	while (nextcell != 'z')
	{
		if(nextcell == opp)
		{
			nextcell = board[row][++i];
			if(nextcell == piece)
			{
				for(int j=column;j<=i;j++)
				{ 
				board[row][j]=piece;
				}
				break;
			}
		}
		else
		{
			break;
		}
	}
}

void Gameboard:: flipLeft(short row, short column)
{
	char nextcell = ' ';
	char opp;
	if(piece == 'O')
		opp = '@';
	else if(piece == '@')
		opp = 'O';
	short i = column-1;
	nextcell = board[row][i];
	while (nextcell != 'z')
	{
		if(nextcell == opp)
		{
			nextcell = board[row][--i];
			if(nextcell == piece)
			{
				for(int j=i;j<column;j++)
				{ 
				board[row][j]=piece;
				}
				break;
			}
		}
		else
		{
			break;
		}
	}
}

